package com.ruoyi.quartz.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.project.system.domain.SysDistribution;
import com.ruoyi.project.system.domain.SysOrder;
import com.ruoyi.project.system.domain.SysTask;
import com.ruoyi.project.system.domain.bo.ExpressBo;
import com.ruoyi.project.system.mapper.SysDistributionMapper;
import com.ruoyi.project.system.mapper.SysOrderMapper;
import com.ruoyi.project.system.mapper.SysTaskMapper;
import com.ruoyi.quartz.task.json.OrderGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 胡乖乖童鞋
 * @date 2022/4/8 21:05
 * @describe
 */
@Component("OrderTask")
public class OrderTask {

  @Autowired private SysOrderMapper sysOrderMapper;
  @Autowired private SysDistributionMapper distributionMapper;
  @Autowired private SysTaskMapper taskMapper;
  /** 生成出库任务 */
  public void Distribution() {
    List<SysOrder> sysOrders = sysOrderMapper.selectSysOrderListByStatusEquireThree();
    if (!sysOrders.isEmpty()) {
      sysOrders.forEach(
          item -> {
            // 获取到订单中的数据
            List<OrderGoods> orderGoods = JSON.parseArray(item.getGoodsList(), OrderGoods.class);
            // 创建出库任务
            SysDistribution sysDistribution = new SysDistribution();

            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("[");
            orderGoods.forEach(
                i -> {
                  stringBuffer.append("{");
                  stringBuffer.append("商品编号：" + i.getGoodsId() + ",");
                  stringBuffer.append("商品数量：" + i.getPrice() + ",");
                  stringBuffer.append("商品名称：" + i.getGoodsName());
                  stringBuffer.append("},");
                });
            stringBuffer.append("]");

            sysDistribution.setDistributionStatus(0);
            sysDistribution.setGoodsShow(stringBuffer.toString());
            sysDistribution.setGoodsList(item.getGoodsList());
            sysDistribution.setCreateTime(new Date());
            sysDistribution.setOrderId(item.getOrderId());
            distributionMapper.insertSysDistribution(sysDistribution);
            // 修改订单状态
            SysOrder sysOrder = new SysOrder();
            sysOrder.setOrderId(item.getOrderId());
            sysOrder.setOrderStatus(4L);
            sysOrderMapper.updateSysOrder(sysOrder);
          });
    }
  }

  /** 1个小时没骑手接单则失败 */
  public void failExpress() {
    SysTask sysTask = new SysTask();
    sysTask.setTaskStatus(0);
    List<ExpressBo> expressBos = taskMapper.selectSysTaskList(sysTask);

    expressBos.forEach(
        item -> {
          // 往后偏移一个小时
          DateTime offset = DateUtil.offset(item.getCreateTime(), DateField.HOUR_OF_DAY, 1);
          if (offset.isBefore(new Date())) {
            // 说明超过时间没人接
            SysTask task = new SysTask();
            task.setTaskId(item.getTaskId());
            task.setTaskStatus(3);
            task.setUpdateTime(new Date());
            taskMapper.updateSysTask(task);

            Long orderId = item.getOrderId();
            SysOrder order = new SysOrder();
            order.setOrderId(orderId);
            order.setOrderStatus(10L);
            order.setBusinessRemark("没有人去配送");
            order.setUpdateTime(new Date());
            sysOrderMapper.updateSysOrder(order);
          }
        });
  }

    /**
     * 清除任务
     */

  /** 发送配送任务 */
  public void express() {
    List<SysOrder> sysOrders = sysOrderMapper.selectSysOrderListByStatusEquireFive();

    sysOrders.forEach(
        item -> {
          SysTask sysTask = new SysTask();
          sysTask.setCreaterId(item.getOwnder());
          sysTask.setExpressId(item.getExpressId());
          sysTask.setTaskStatus(0);
          sysTask.setTaskRecord(item.getRemark());
          sysTask.setAddressId(item.getAddressId());
          sysTask.setOrderId(item.getOrderId());
          sysTask.setCreateTime(new Date());
          taskMapper.insertSysTask(sysTask);

          item.setOrderStatus(12L);
          item.setUpdateTime(new Date());
          sysOrderMapper.updateSysOrder(item);
        });
  }
}
